package com.tsunami.authority.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsunami.authority.entity.auth.User;
import com.tsunami.authority.model.SecurityUser;
import com.tsunami.authority.provider.AdminProvider;
import com.tsunami.base.R;
import com.tsunami.common.MapConstant;
import com.tsunami.enums.Sex;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.utils.TimeUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lx
 * @date 2021-09-28 15:08
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private AdminProvider adminProvider;

    @Override
    public UserDetails loadUserByUsername(String s) {
        R userStr = adminProvider.selectByAccount(s);

        Map<String, Object> userMap = userStr.getExtra();
        if (!userStr.getCode().equals(ExceptionCode.SUCCESS.getCode()) || userMap.get(MapConstant.MAP_DATA) == null) {
            throw new BizException(ExceptionCode.NO_USER);
        }
        User user = getUserByObject(userMap.get(MapConstant.MAP_DATA));
        R roleResult = adminProvider.findRolesByUser(user.getId());
        if (null != roleResult && roleResult.getCode().equals(ExceptionCode.SUCCESS.getCode())) {
            Map roleMap = roleResult.getExtra();
            List<String> rolesList = (List<String>) roleMap.get("data");
            SecurityUser loginUser = new SecurityUser();
            loginUser.setUser(user);
            loginUser.setPermissions(rolesList);

            return loginUser;
        }
        return null;
    }

    User getUserByObject(Object o) {
        JSONObject userObject = (JSONObject) JSON.toJSON(o);
        User user = new User();
        user.setId(userObject.getLong("id"));
        user.setAccount(userObject.getString("account"));
        user.setAvatar(userObject.getString("avatar"));
        user.setEmail(userObject.getString("email"));
        user.setMobile(userObject.getString("mobile"));
        user.setName(userObject.getString("name"));
        user.setOrgId(userObject.getLong("orgId"));
        user.setPassword(userObject.getString("password"));
        user.setPasswordErrorNum(userObject.getInteger("passwordErrorNum"));
        JSONObject sex = userObject.getJSONObject("sex");
        if (sex != null && sex.get(MapConstant.MAP_CODE).equals(Sex.M.getCode())) {
            user.setSex(Sex.M);
        } else if (sex != null && sex.get(MapConstant.MAP_CODE).equals(Sex.W.getCode())) {
            user.setSex(Sex.W);
        } else {
            user.setSex(Sex.N);
        }
        user.setPasswordExpireTime(TimeUtil.stringToJava8Time((String) userObject.get("passwordExpireTime")));
        user.setAccountExpireTime(TimeUtil.stringToJava8Time((String) userObject.get("accountExpireTime")));
        user.setStationId(userObject.getLong("stationId"));
        user.setStatus(userObject.getString("status"));
        user.setWorkDescribe(userObject.getString("workDescribe"));
        user.setCreateUser(userObject.getLong("createUser"));
        user.setPasswordErrorLastTime(TimeUtil.stringToJava8Time((String) userObject.get("passwordErrorLastTime")));
        return user;
    }
}
